package easy;

import util.PrintUtils;

import java.util.ArrayList;
import java.util.List;

public class Solution_1078 {

    public String[] findOcurrences(String text, String first, String second) {
        String s = first + " " + second + " ";
        List<String> list = new ArrayList<>();
        int index, offset = 0;
        if (text.startsWith(s)) {
            index = 0;
        } else {
            index = text.indexOf(" " + s);
            offset = 1;
        }
        while (index >= 0) {
            for (int i = index + s.length() + offset; i < text.length(); i++) {
                if (text.charAt(i) == ' ') {
                    list.add(text.substring(index + s.length() + offset, i));
                    break;
                } else if (i == (text.length() - 1)) {
                    list.add(text.substring(index + s.length() + offset));
                    break;
                }
            }
            text = text.substring(index + first.length() + 1);
            if (text.startsWith(s)) {
                index = 0;
                offset = 0;
            } else {
                index = text.indexOf(" " + s);
                offset = 1;
            }
        }
        return list.toArray(new String[0]);
    }

    public static void main(String[] args) {
        Solution_1078 model = new Solution_1078();
        PrintUtils.array(model.findOcurrences("alice is a good girl she is a good student a good", "a", "good"));
        PrintUtils.array(model.findOcurrences("we will we will rock you", "we", "will"));
        PrintUtils.array(model.findOcurrences("alice is aa good girl she is a good student", "a", "good"));
        PrintUtils.array(model.findOcurrences("we we we we will rock you","we","we"));
    }
}
